
Task=function(input){
  var self=this;
  self.input=input;

  self.actions=Array();
  
  self.parseInput=function(){
    var actions=self.input.split(',');
    self.init=parseInt(actions[0].split('=')[1]);
    self.deadline=parseInt(actions[1].split('=')[1]);
    for(var i=2;i<actions.length;i++){
      var sep=actions[i].split('=');
      self.actions.push([sep[0],parseInt(sep[1])]);
    }
  }
  self.parseInput();
}

TaskState=function(task,id){
  var self=this;

  self.state='not inited';
  self.prioDyn=-1;
  self.execTimeRunning=0;
  self.execTimeTotal=0;
  self.totalTime=0;
  self.actions=task.actions;
  self.action='none';
  self.init=task.init;
  self.deadline=task.deadline;
  self.lastState='none';
  self.id=id;
  self.RIP=0;

  self.quantum=self.init+1;
  self.indexAction=-1;

  self.setPrioBase=function(prio){
    self.prioBase=prio;
    return self;
  }

  self.setPrioDyn=function(prio){
    self.prioDyn=prio;
    return self;
  }

  self.getAction=function(){
    return self.action;
  }

  self.getState=function(){
    return self.state;
  }

  //performs execution during one unit of time
  self.tick=function(){
    self.totalTime++;
    if(self.state=='running'){
      self.execTimeTotal++;
      self.execTimeRunning++;
      self.quantum--;
    }
    else if(self.state=='suspended' || self.state=='blocked')
      self.execTimeTotal++;
    else if(self.state=='not inited')
      self.quantum--;
  }
  //changes the state of the task
  self.changeState=function(newState){
    self.state=newState;
  }
  //returns the total time in execution of the task
  self.getTimeRunning=function(){
    return self.execTimeRunning;
  }
  self.getTimeTotal=function(){
    return self.execTimeTotal;
  }
  self.nextAction=function(){
    self.indexAction++;
    if(self.indexAction>=self.actions.length){
      self.changeState('finished');
      self.setPrioDyn(-1);
    }
    else{
      self.lastState=self.getState();
      self.action=self.actions[self.indexAction][0]
      self.quantum=self.actions[self.indexAction][1];
      self.changeState('suspended');
    }
  }
}
